import re
from bcat.util.str import Str

class IValidator(object):
    def is_ok(self):
        return False
    def is_failed(self):
        return not self.is_ok()

    def __bool__(self):
        return self.is_ok()

class Validator(object):
    @staticmethod
    def email(s) -> IValidator: 
        return EmailValidator(s)


class EmailValidator(IValidator):
    def __init__(self, s):
        self.__str = s

    def is_ok(self):
        if type(self.__str) == str and Str(self.__str).is_not_empty():
            return self.__validate_email()
        return False
    
    def __validate_email(self):
        pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
        if re.fullmatch(pattern, self.__str):
            return True
        return False


import unittest
class TestUtility(unittest.TestCase):
    def test_email(self):
        s = "abc@abc.com"
        self.assertTrue(Validator.email(s).is_ok())
        self.assertFalse(Validator.email(s).is_failed())
        self.assertTrue(Validator.email(s))

        s = "abc@"
        self.assertFalse(Validator.email(s).is_ok())
        self.assertFalse(Validator.email(s))

        s = "@"
        self.assertFalse(Validator.email(s).is_ok())
        self.assertFalse(Validator.email(s))

        s = ""
        self.assertFalse(Validator.email(s).is_ok())
        self.assertFalse(Validator.email(s))

        s = None
        self.assertFalse(Validator.email(s).is_ok())
        self.assertFalse(Validator.email(s))

        s = 12345
        self.assertFalse(Validator.email(s).is_ok())
        self.assertFalse(Validator.email(s))

    def test_string(self):
        #Validator.string(s).all_digital();
        pass
    
